package mygang.controller.manager;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import mygang.controller.AbstractController;
import mygang.helper.ControllerHelper;
import mygang.helper.JsonHelper;
import mygang.helper.Utils;
import mygang.model.constructor.TableWizardModel;
import mygang.service.manager.TableService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 *
 * @author Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
 * date: 23/02/2013
 */
@Controller
@RequestMapping(AbstractController.MANAGE_TABLE)
public class TableController extends AbstractManageController<TableService, TableWizardModel> {

    private static final Logger log = Logger.getLogger(TableController.class.toString());

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public void getTablesList(HttpServletResponse response) throws IOException {
        try {
            JsonHelper.prepareJsonResponse(Utils.convertToOptions(getService().getTableList()), response);
        } catch (Exception ex) {
            ControllerHelper.handleException(ex, response);
        }
    }
    
    @RequestMapping(value = "/getTableColumns", method = RequestMethod.POST)
    public void getTableColumns(@RequestParam(required = true, value = "id") Long id, HttpServletResponse response) throws IOException {
        try {
            TableWizardModel table = getService().getById(id.longValue());
            JsonHelper.prepareJsonResponse(Utils.convertToOptions(getService().getTableColumns(table.getName())), response);
        } catch (Exception ex) {
            ControllerHelper.handleException(ex, response);
        }
    }
    
    @RequestMapping(value = "/unique", method = RequestMethod.POST)
    public void getTablesUniqueList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            JsonHelper.prepareJsonResponse(Utils.convertToOptions(getService().getUniqueList()), response);
        } catch (Exception ex) {
            ControllerHelper.handleException(ex, response);
        }
    }

    public TableWizardModel prepareObject(HttpServletRequest request) throws Exception {
        String tempId = request.getParameter("id");
        Long id = tempId!=null && !tempId.isEmpty() ? new Long(request.getParameter("id")) : null;
        
        String name = request.getParameter("name");
        if(name==null || name.isEmpty()) throw new Exception("Name is not specified");
        String desc = request.getParameter("desc");
        
        TableWizardModel model;
        if(id==null){
            String pk = getService().getTablePrimaryColumn(name);
            model = new TableWizardModel(name, pk, desc);
        } else {
            model = getService().getById(id);
            model.setDescription(desc);
        }
        return model;
    }
    
    String getLink() {
        return MANAGE_TABLE;
    }
}